//
//  FSMainViewController.swift
//  fushp_weibo
//
//  Created by fushp on 2017/11/9.
//  Copyright © 2017年 fushp. All rights reserved.
//

import UIKit

class FSMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setupChildControllers()
        setcenterBtn()
        // Do any additional setup after loading the view.
    }
    /*
         使用代码控制设备的方向 可以在需要横屏的时候 单独处理
         使用播放视频  可以通过 modal 展示 就是用present 这个时候可以旋转
     */
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask
    {
            return .portrait
    }
    
    //mark: --私有控件
    private lazy var compnentBtn : UIButton = UIButton(type: UIButtonType.system)
    //创建点击方法 @objc允许在运行时 通过 oc 消息机制被调用
    @objc private func composeState() {
        print("点击按钮")
        //测试modal 是否可以横屏
//        let vc = UIViewController()
//        vc.view.backgroundColor = UIColor.randomColor
//        let nav = UINavigationController(rootViewController: vc)
//        present(nav, animated: true, completion: nil)
        
    }
}

//extension 类似于oc中的分类  在 swift 中还可以用来切分代码块
//可以把相近功能的函数   放在一个extension 中
//便于维护
extension FSMainViewController {
    //center 按钮
    private func setcenterBtn() {//tabbar_compose_icon_add
        compnentBtn.setImage(UIImage(named:"tabbar_compose_icon_add"), for: .normal)
        compnentBtn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), for: .highlighted)
        compnentBtn.setBackgroundImage(UIImage(named:"tabbar_compose_button")?.withRenderingMode(.alwaysOriginal), for: .normal)

    tabBar.addSubview(compnentBtn)
    let count = CGFloat(childViewControllers.count)
        let width = tabBar.bounds.width / count - 1
    //正数向内缩放 负数向外扩展
    compnentBtn.frame = tabBar.bounds.insetBy(dx: 2*width, dy: 0)
        compnentBtn .addTarget(self, action:#selector(composeState), for: .touchUpInside)
        
        
    }
    //设置所有子控制器
    private func setupChildControllers() {
        //从沙盒中读取数据 xcode 加入json文件 需要在build phase 中配置bundle 路径
        let dirpath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let jsonpath = dirpath.appending("/main.json")
         var data  = NSData(contentsOfFile: jsonpath)
        if data == nil {
           let path = Bundle.main.path(forResource: "main", ofType: "json")
             data = NSData(contentsOfFile: path!)
        }
        guard let array = try! JSONSerialization.jsonObject(with: data! as Data, options: [])  as? [[String : AnyObject]]
        else  {
                return
        }
        
//        let ducumentPath2 = NSHomeDirectory() + "/Documents/demo.json"
//        let  is_success = ( data as NSData).write(toFile:"/Users/fushp/Desktop/demo.json", atomically: true)
//        if !is_success {
//            print("fail")
//        }
        var arrays = [UIViewController]()
        for dic in array {
            arrays.append(didcotroller(dict: dic as [String : AnyObject]))
        }
        viewControllers = arrays
        //数据转json
       

    }
    //使用字典创建一个子控制器
    private func didcotroller(dict: [String: AnyObject]) -> UIViewController {
        guard let classNmae = dict["clsName"] as? String,
            let imageName = dict["imageName"] as? String,
            let title = dict["title"]  as? String,
            let cls = NSClassFromString(Bundle.main.namespace + "." + classNmae) as? FSBaseViewController.Type,
            let visitordic  = dict["visitorInfo"] as? [String: String]
        else {
            return UIViewController()
        }
        
        let vc = cls.init()
        vc.title = title
        vc.tabBarItem.image = UIImage(named: "tabbar_"+imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_"+imageName+"_highlighted")?.withRenderingMode(.alwaysOriginal)
        vc.visitorInfo = visitordic
        //修改字体大小
        
        vc.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.orange],
                                             for: .highlighted)
        vc.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.font: UIFont.systemFont(ofSize: 12)], for: UIControlState(rawValue: 0))
        let nav = FSNavigationController(rootViewController: vc)
        return nav
       
    }
    
}
